package cn.com.twoke.get_xml_data.service.impl;

import cn.com.twoke.get_xml_data.entity.TestPlanReport;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.io.File;
import java.text.MessageFormat;

@Slf4j
@RequiredArgsConstructor
public class LogGetXmlDataHandler extends DefaultGetXmlDataHandler {

    private final JTextArea logTextArea;

    @Override
    protected void handle(String fileNo, File file, TestPlanReport testPlanReport) {
      String logInfo = "\n=================开始 {0} 执行=================\n" +
              "fileNo: {1}\n"+
 "fileName: {2}\n"+
 "filePath: {3}\n"+
 "data: {4}\n"+
 "=================结束 {0} 执行=================\n";
      String title = fileNo + " " +file.getName();
      String format = MessageFormat.format(logInfo, title,
                fileNo,
                file.getName(),
                file.getAbsolutePath(),
              JSONUtil.toJsonStr(testPlanReport));
      log.info(format);
      if (logTextArea != null) {
          synchronized (logTextArea) {
              logTextArea.append(format);
          }
      }
    }

}
